<?php
namespace app\index\controller;
use think\Controller;

use app\index\model\User as UserModel;
use think\Session;


header("Content-type: text/html; charset=utf-8");
//验证类

//接收登录页面表单信息


//登录页面展示
class Info extends Base{

    public function index(){


        $UserID = Session::get('userid');
        $UserName = Session::get('username');

        //dump($UserID);die;
        //初始化模型
        $User = new UserModel;
        $data = $User->where('id',$UserID)->find();

        $this->assign('data',$data);



        $this->assign('username',$UserName);
        $this->assign('userid',$UserID);


        //dump($data);

        return $this->fetch();
    }

    //接受个人信息的修改表单
    public function indexForm(){

        $UserInfo = input('post.');

        //dump($UserInfo);

        //存入
        //初始化模型

        $userModel = new UserModel();

        $Reslut = $userModel->save([

            'sex' => $UserInfo['sex'],
            'identitynum' => $UserInfo['identitynum'],
            'email' => $UserInfo['email'],
            'phone' => $UserInfo['phone']
        ],[
            'id' => $UserInfo['userid']
        ]);


        //return $this->redirect('Come/index',array('id' => $data['id'],'username' => $data['username']));
        if($Reslut){
            //修改成功
            $this->redirect('Come/index');
        }else{
            //修改失败

            $this->error('修改失败',"Info/index");
        }
    }

    //展示修改密码页面
    public function resetPwd(){

        $UserID = Session::get('userid');
        $UserName = Session::get('username');

        $this->assign('username',$UserName);
        $this->assign('userid',$UserID);

        // echo "登录页面";
        return $this->fetch();
    }

    //接收登录页面表单信息
    public function resetPwdForm(){


        /*$UserInfo = input('param.');
        $UserName = $UserInfo['username'];
        $UserID = $UserInfo['userid'];

        $this->assign('username',$UserName);
        $this->assign('userid',$UserID);*/


        $resetPwd = input('post.');
        /*dump($resetPwd);
        die;*/

        //给出模型
        $userModel = new UserModel();
        $userResult = $userModel->where('id',$resetPwd['userid'])->find();
        //转为数组
        $data = $userResult->getData();
        //dump($data);die;
        //修改二级密码
        /*if($resetPwd['type'] == 'on' && $data['savepwd'] == md5($resetPwd['password'])){
            //可以修改二级密码

        }else{
            //不允许修改


        }*/

        //比较密码
        if($data['password'] == md5($resetPwd['password'])){
            //比较正确,可以修改密码
            //echo "密码正确";


            $Reslut = $userModel->save(['password' => $resetPwd['Newpassword']],['id' => $resetPwd['userid']]);
            if($Reslut){
                 $this->success("修改成功");
            }else{
                 $this->error("修改失败");
            }
            //exit;
        }else{
            //错误,不许修改密码
            //echo "密码错误";
            $this->error("密码错误！！！修改失败");
            //exit;
        }

    }

    //跳转设置安全问题
    public function redirectRegister(){
        $this->redirect("Register/index");
    }

    //忘记密码页面展示
    public function forgotpass(){
        //echo 11;die;
        return $this->fetch();
    }

    public function forgotpassForm(){

        $ForgotPassEmail = input('post.');
        //dump($ForgotPassEmail);

        //得到账号跟email
        //给出模型
        $userModel = new UserModel();
        $userResult = $userModel->where('username',$ForgotPassEmail['username'])->find();
        //转为数组
        $data = $userResult->getData();
        //dump($data);die;
        //比较邮箱
        if($data['email'] == $ForgotPassEmail['email']){
            //比较正确,发送邮件模块
            // echo "邮箱正确";
            //exit;
        }else{
            //错误,跳error
            // echo "邮箱错误";
            //exit;
        }

        //发送消息给email
    }

}
?>
